<template>
  <div class="my_progress" :style="{'--be':color.be,'--af':color.af,'--main':color.main}">
    <div class="t">
      <div class="start_time">{{start_time | timestampToTime}}</div>
      <div class="end_time">{{end_time | timestampToTime}}</div>
    </div>
    <div class="b">
      <div class="line">
        <div class="fill" :style="{width:`${bfb}%`}"></div>
      </div>
    </div>
  </div>
</template>

<script>
const colors = [
  {main:'#409eff',be:'#58a2fa',af:'#96c2fa'},
  {main:'#5cb87a',be:'#5b966d',af:'#96b69d'},
  {main:'#f56c6c',be:'#d07070',af:'#de9797'}
]
export default {
  name: "MyProgress",
  props:{
    start_time:String,
    end_time:String
  },
  data(){
    return {
      bfb:'0%',
      color:{main:'#409eff',be:'#58a2fa',af:'#96c2fa'}
    }
  },
  created() {
    setTimeout(()=>{
      let now = new Date().getTime()
      if(now >= this.end_time){
        this.bfb = 100
      }else{
        this.bfb = (1-((this.end_time-0)-now)/(this.end_time-0-this.start_time))*100
      }
      if(this.bfb>=80){
        this.color = colors[2]
      }else if(this.bfb>=40){
        this.color = colors[1]
      }
    },100)
  },
  filters:{
    timestampToTime(timestamp){
      let date = new Date(parseInt(timestamp));
      let Y = date.getFullYear() + '-';
      let M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
      let D = (date.getDate() < 10 ? '0'+date.getDate() : date.getDate()) + ' ';
      let h = (date.getHours()>=10?date.getHours():'0'+date.getHours()) + ':';
      let m = (date.getMinutes()>=10?date.getMinutes():'0'+date.getMinutes()) + ':';
      let s = date.getSeconds()>=10?date.getSeconds():'0'+date.getSeconds();
      return Y+M+D+h+m+s;
    },
  },
}
</script>

<style scoped>
.my_progress{
  width: 100%;
  height: 40px;
  margin-top: 10px;
  padding: 0 10px;
}
.my_progress .t{
  width: 100%;
  height: 20px;
  display: flex;
  justify-content: space-between;
}
.my_progress .b{
  width: 100%;
  height: 20px;
  margin: 0 auto;
}
.my_progress .b .line{
  width: 90%;
  height: 6px;
  background: #ebeef5;/*#409eff*/
  margin: 5px auto 0;
  border-radius: 10px;
}
.my_progress .b .line .fill{
  width: 0%;
  height: 6px;
  background: var(--main);
  border-radius: 10px;
  position: relative;
  transition: all .8s;
}
.my_progress .b .line .fill:before{
  content: '';
  width: 14px;
  height: 14px;
  background: var(--be);
  border-radius: 50%;
  position: absolute;
  right: 0;
  top: -4px;
  transition: all 1s;
  animation: lss 1s linear infinite alternate;
}
@keyframes lss {
  0%{
    transform: scale(1);
    background: var(--be);
  }
  100%{
    transform: scale(1.05);
    background: var(--af);
  }
}
@media all and (max-width: 768px) {
  .my_progress{
    width: 100%;
    height: 60px;
  }
  .my_progress .t{
    width: 100%;
    height: 40px;
    display: flex;
    justify-content: space-between;
  }
  .my_progress .t .start_time{
    width: 80px;
    line-height: 20px;
    text-align: center;
  }
  .my_progress .t .end_time{
    width: 80px;
    line-height: 20px;
    text-align: center;
  }
}
</style>
